package com.book.lizhi.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.model.SongModel;

public class SongAdapter extends AppAdapter {

	public SongAdapter(Context context, List<?> list) {
		super(context, list);
	}

	@Override
	public View getView(int position, View convertView, final Context context) {
		final Viewholder holder;
		if (convertView == null) {
			holder = new Viewholder();
			convertView = View.inflate(context, R.layout.film_item_layout, null);
			holder.nameView = (TextView) convertView.findViewById(R.id.film_name);
			holder.desView = (TextView) convertView.findViewById(R.id.film_des);
			holder.reviewView = (TextView) convertView.findViewById(R.id.film_review);
			holder.goodView = (TextView) convertView.findViewById(R.id.film_good_view);
			holder.goodContainer = convertView.findViewById(R.id.favorite_container);
			holder.shareView = (TextView) convertView.findViewById(R.id.article_share_view);
			holder.shareContainer = convertView.findViewById(R.id.share_container);
			convertView.setTag(holder);
		} else {
			holder = (Viewholder) convertView.getTag();
		}
		final SongModel model = (SongModel) getItem(position);
		holder.nameView.setText(model.name);
		holder.desView.setText(model.brief);
		holder.reviewView.setVisibility(View.GONE);
		
		if (model.isFav) {
			holder.goodView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good_press, 0, 0, 0);
		} else {
			holder.goodView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good, 0, 0, 0);
		}

		holder.goodContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (model.isFav) {
					holder.goodView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good, 0, 0, 0);
					model.isFav = false;
					// DataBaseMannager.getInstance().deleteFavById(FilmDetailActivity.this, mArticleModel.id);
					Intent intent = new Intent(Constants.DELETE_FAV);
					intent.putExtra("id", model.id);
					LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
				} else {
					holder.goodView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.iconfont_good_press, 0, 0, 0);
					model.isFav = true;
					// DataBaseMannager.getInstance().insertFave(FilmDetailActivity.this, mArticleModel);
					Intent intent = new Intent(Constants.FAV);
					intent.putExtra("id", model.id);
					LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
				}
			}
		});
		holder.shareContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT, "我在励志应用中看到一篇很有用的文章《" + model.name + "》下载励志应用看完整文章。http://www.wandoujia.com/apps/com.book.lizhi");
				sendIntent.setType("text/plain");
				context.startActivity(Intent.createChooser(sendIntent, "励志文章分享"));
			}
		});

		return convertView;
	}

	class Viewholder {
		public View goodContainer;
		public TextView goodView;
		public View shareContainer;
		public TextView shareView;
		public TextView nameView;
		public TextView desView;
		public TextView reviewView;
	}

}
